package design.p16_iterator;

/**
 * @ClassName: Java
 * @description:
 * @author: zxf
 * @create: 2021-06-19 12:53
 **/
public class MyIterator implements Iterator{

    private Collection collection;
    private int pos = -1;

    public MyIterator(Collection collection){
        this.collection = collection;
    }

    @Override
    public Object previous() {
        if (pos > 0) pos--;
        return collection.get(pos);
    }

    @Override
    public Object next() {
        if (pos < collection.size()-1) pos++;
        return collection.get(pos);
    }

    @Override
    public boolean hasNext() {
        return pos < collection.size()-1?true:false;
    }

    @Override
    public Object first() {
        pos = 0;
        return collection.get(pos);
    }
}

